package com.sogoodsoft.web.project;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.project.PrjFile;
import com.sogoodsoft.service.project.PrjFileService;
import com.sogoodsoft.util.InfosysUtils;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/project")
public class PrjFileAction extends CrudActionSupport < PrjFile > {

    /**
	 * 
	 */
    private static final long serialVersionUID = -6319412370522018977L;

    private PrjFile prjFile;

    private File docFile;

    private String title;

    private Long id;

    private PrjFileService prjFileSer;

    @Override
    public String delete() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String input() throws Exception {
        // TODO Auto-generated method stub
        return INPUT;
    }

    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        // TODO Auto-generated method stub
        if (id != null) {
            prjFile = prjFileSer.get(id);
        } else {
            prjFile = new PrjFile();
        }
    }

    /**
     * 保存文件
     * 
     * @return
     */
    public String saveFile() {

        logger.debug("saveFile");
        long length = docFile.length();
        boolean success = true;
        try {
            if (length > 0) {
                // 保存文件
                title = new String(title.getBytes("ISO8859-1"), "UTF-8");
                File newFile;
                if (id == null) {
                    prjFile.setFilepath(WebConstants.DOC_DIR);
                    prjFile.setOriginalName(title + ".doc");
                    prjFile.setStoreName(title + InfosysUtils.getNow() + ".doc");
                    prjFile.setUploadTime(new Date());
                    String fileName = InfosysUtils.getAbosluteFilePath(prjFile.filepath + prjFile.storeName);
                    logger.debug(fileName);
                    newFile = new File(fileName);
                    newFile.createNewFile();
                } else {
                    prjFile.setFilepath(WebConstants.DOC_DIR);
                    prjFile.setOriginalName(title);
                    prjFile.setUploadTime(new Date());
                    String fileName = InfosysUtils.getAbosluteFilePath(prjFile.getFilepath() + prjFile.getStoreName());
                    logger.debug(fileName);
                    newFile = new File(fileName);
                }
                FileInputStream fin = new FileInputStream(docFile);
                FileOutputStream fout = new FileOutputStream(newFile);

                byte buffer[] = new byte[1000];
                int len;
                while ((len = fin.read(buffer)) > 0) {
                    fout.write(buffer, 0, len);
                }
                fin.close();
                fout.close();

                if (prjFile.getId() != null) {
                    prjFileSer.save(prjFile);
                } else {
                    // 文件全名存入session中
                    // 关联父类保存
                    PrjFile[] prjFiles = new PrjFile[] { prjFile };
                    ActionContext.getContext().getSession().put("prjFiles", prjFiles);
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        }

        if (success) {
            Struts2Utils.renderText("true");
        } else {
            Struts2Utils.renderText("false");
        }

        logger.debug("File Length:" + length);
        return null;
    }

    @Override
    public String save() throws Exception {
        // TODO Auto-generated method stub
        saveFile();
        return null;
    }

    /**
     * 文件输出action
     * 
     * @return
     * @throws Exception
     */
    public String getFile() throws Exception {
        this.prepareModel();
        HttpServletResponse response = Struts2Utils.getResponse();
        response.setContentType("application/msworld");
        String file = prjFile.getFilepath() + prjFile.getStoreName();
        file = InfosysUtils.getAbosluteFilePath(file);
        File outFile = new File(file);
        FileInputStream fin = new FileInputStream(outFile);
        OutputStream fout = response.getOutputStream();
        byte[] buffer = new byte[1000];
        int bytes = 0;
        while ((bytes = fin.read(buffer)) > 0) {
            fout.write(buffer, 0, bytes);
        }
        fin.close();
        fout.close();
        return null;
    }

    @Override
    public PrjFile getModel() {
        // TODO Auto-generated method stub
        return prjFile;
    }

    public PrjFile getPrjFile() {
        return prjFile;
    }

    public void setPrjFile(PrjFile prjFile) {
        this.prjFile = prjFile;
    }

    public File getDocFile() {
        return docFile;
    }

    public void setDocFile(File docFile) {
        this.docFile = docFile;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public PrjFileService getPrjFileSer() {
        return prjFileSer;
    }

    @Autowired
    public void setPrjFileSer(PrjFileService prjFileSer) {
        this.prjFileSer = prjFileSer;
    }

}
